import useStore from "@/store";
import { createRouter, createWebHashHistory, useRouter } from "vue-router";
const routes = [
    {
        path: '/',
        component: () => import('../views/main.vue'),
        name: 'home1',
        redirect: '/home',
        children: []
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/login/index.vue')
    },

]
const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    const store = useStore();
    const token = store.token;
    const routesList = window.sessionStorage.getItem('menu');

    // 如果目标路由是登录页面，直接放行
    if (to.path === '/login') {
        next();
    } else {
        // 如果动态路由未加载，加载动态路由
        if (!store.isRoutesLoaded) {
            store.addMenu(router); // 动态添加路由
            store.isRoutesLoaded = true; // 标记路由已加载
            next(to.fullPath); // 重新跳转到目标路由
        } else {
            // 如果动态路由已加载，直接放行
            next();
        }
    }
});

export default router